import React from 'react'
import { Button, Modal, Select, Form, Input, message } from 'antd';
import { ruchangshouquan, chuchangshouquan } from "../service"
const From = ({ ItemData }) => {


  const [form] = Form.useForm();

  const onFinish = (values) => {
    console.log('Success:', values);
    ruchangshouquan({
      "youxiaoqi": values.validity,
      "youxiaoqineideng": values.validityLight,
      "youxiaoqiwaideng": values.validityLightOut,
      "source": 1,
      "guihao": ItemData.locker_number,
      "uuid": new Date().getTime().toString(),
      "lockclass": ItemData.locker_type,

    }).then(res => {
      if (res.result == 0) {
        message.success(ItemData.locker_number + "入场授权成功")
      } else {
        message.error("授权失败 : " + res.msg)
      }
    }).catch(err => {
      message.error("授权失败 : " + err.message)
    })
  };



  const Out = () => {
    form.validateFields().then(values => {

      chuchangshouquan({
        "youxiaoqi": values.validity,
        "youxiaoqineideng": values.validityLight,
        "youxiaoqiwaideng": values.validityLightOut,
        "source": 1,
        "guihao": ItemData.locker_number,
        "uuid": new Date().getTime().toString(),
        "lockclass": ItemData.locker_type,
      }).then(res => { 
        if (res.result == 0) {
          message.success(ItemData.locker_number + "离场授权成功")
        } else {
          message.error("授权失败 : " + res.msg)
        }
      }).catch(err => {
      message.error("授权失败 : " + err.message)
    })
    })
  }

  const onFinishFailed = (errorInfo) => {
    console.log('Failed:', errorInfo);
  };

  return (
    <div>
      <Form
        name="basic"
        labelCol={{ span: 5 }}
        wrapperCol={{ span: 13 }}
        style={{ maxWidth: 700 }}
        initialValues={{ remember: true }}
        onFinish={onFinish}
        onFinishFailed={onFinishFailed}
        autoComplete="off"
        form={form}
      >
        <Form.Item
          label="有效期(分钟)"
          name="validity"
          initialValue={10}
          rules={[{ required: true, message: '请输入有效期' }]}
        >
          <Input />
        </Form.Item>

        <Form.Item
          label="有效期内灯"
          name="validityLight"
          rules={[{ required: true, message: 'Please input your password!' }]}
          initialValue={'1'}
        >
          <Select style={{ minWidth: 160 }} placeholder="请选择柜类型">
            <Select.Option value="0">熄灭</Select.Option>
            <Select.Option value="1">闪烁</Select.Option>
            <Select.Option value="2">常亮</Select.Option>
          </Select>
        </Form.Item>

        <Form.Item
          label="有效期外灯"
          name="validityLightOut"
          rules={[{ required: true, message: 'Please input your password!' }]}
          initialValue={'0'}
        >
          <Select style={{ minWidth: 160 }} placeholder="请选择柜类型">
            <Select.Option value="0">熄灭</Select.Option>
            <Select.Option value="1">闪烁</Select.Option>
            <Select.Option value="2">常亮</Select.Option>
          </Select>
        </Form.Item>

        <Form.Item label={null} wrapperCol={{ offset: 5, span: 10 }} >
          <Button type="primary" htmlType="submit">
            入场授权
          </Button>
          &nbsp;&nbsp;&nbsp;
          <Button type="danger" onClick={() => Out()}>
            离场授权
          </Button>
        </Form.Item>
      </Form>
    </div>
  )
}

export default From
